package com.detection.controller;

import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.utils.HttpUtils;
import com.detection.dahua.config.OauthConfigUtil;
import com.detection.dahua.demo.video.realTimePreview.RealTimePreviewDemo;
import com.detection.dahua.model.brm.device.DeviceTreeRequest;
import com.detection.dahua.model.datasync.DataSync;
import com.detection.dahua.model.video.realTimePreview.HlsUrlRequest;
import com.detection.dahua.model.video.realTimePreview.HlsUrlResponse;
import com.detection.dahua.service.IDahuaDeviceService;
import com.detection.tcp.client.NettyTcpClient;
import com.detection.tcp.domain.TCPMessage;
import com.detection.yolo.service.CameraDetectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@Api(tags = "dahuaDevice")
@RestController
@RequestMapping("/dahuaDevice")
public class DahuaDeviceController {

    @Autowired
    private RealTimePreviewDemo realTimePreviewDemo;

    @Autowired
    private CameraDetectionService cameraDetectionService;

    /**
     * 测试获取设备树
     *
     * @return
     */
    @ApiOperation("启动视频线程")
    @PostMapping("/startDahuaDevice")
    public void startDahuaDevice(String indexCode,String cameraName,String channelId,String streamType,String type,String supportAlgorithms) throws ClientException {
        if(Objects.isNull(channelId)){
            throw new RuntimeException("通道ID不能为空！！");
        }
        if(Objects.isNull(streamType)){
            throw new RuntimeException("码流类型不能为空！！");
        }
        if(Objects.isNull(type)){
            throw new RuntimeException("协议类型不能为空！！");
        }
        String token = HttpUtils.getToken(OauthConfigUtil.getOauthConfig()).getAccess_token();
        HlsUrlRequest hlsUrlRequest = new HlsUrlRequest();
        HlsUrlRequest.Data data = new HlsUrlRequest.Data();
        data.setChannelId(channelId);
        data.setStreamType(streamType);
        data.setType(type);
        hlsUrlRequest.setData(data);
        HlsUrlResponse hlsUrlResponse = realTimePreviewDemo.getHlsUrl(hlsUrlRequest);
        String hlsPath = hlsUrlResponse.getData().getUrl();
        hlsPath += "?token=" + token;
        if(Objects.isNull(cameraName)){
            cameraName = channelId;
        }
        cameraDetectionService.startDetection(indexCode, cameraName, hlsPath,supportAlgorithms);
    }

}
